import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

///
/// 通用开关按钮组件
/// 来源 https://github.com/iampawan/Flutter-UI-Kit/blob/master/lib/ui/widgets/common_switch.dart
///
class DDSwitch extends StatelessWidget {
  final bool defaultValue;
  final Function onChanged;
  DDSwitch({this.defaultValue = false, this.onChanged});

  @override
  Widget build(BuildContext context) {
    return defaultTargetPlatform == TargetPlatform.android
        ? Switch(
            value: defaultValue,
            onChanged: _onChanged,
          )
        : CupertinoSwitch(
            value: defaultValue,
            onChanged: _onChanged,
          );
  }

  Function _onChanged(bool value) {
    return (onChanged != null) ? onChanged(value) : (value) {};
  }
}
